package com.hnkywl.sts.service.kcgl;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hnkywl.sts.dao.kcgl.LldDao;
import com.hnkywl.sts.dao.kcgl.LldmxDao;
import com.hnkywl.sts.dao.xsgl.KhDao;
import com.hnkywl.sts.entity.xsgl.Kh;
import com.hnkywl.sts.entity.xsgl.Lld;
import com.hnkywl.sts.entity.xsgl.Lldmx;
import com.hnkywl.sts.service.base.BmService;
import com.hnkywl.sts.service.sys.YgService;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-5-9
 * Time: 下午9:07
 * To change this template use File | Settings | File Templates.
 */
@Service
public class LldService {
    private LldDao lldDao;
    private LldmxDao lldmxDao;
    private KhDao khDao;
    private BmService bmService;

    /**
     * 查询所有的到货单
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Transactional
    public String findAllLld(String zt, Date kdrqStart, Date kdrqEnd, Long flgsId, String flgsJc, int pageNo, int pageSize) {
        Page<Lld> LldPage = this.lldDao.findLldList(zt, kdrqStart, kdrqEnd, flgsId, flgsJc, pageNo, pageSize);
        List<Lld> LldList = LldPage.getResult() == null ? new ArrayList<Lld>() : LldPage.getResult();
        return "{\"total\":" + LldPage.getTotalCount() + ",\"rows\":" + new GsonBuilder().setDateFormat("yyyy-MM-dd").create().toJson(LldList) + "}";
    }

    /**
     * 根据Id查询领料单
     *
     * @param id
     * @return
     */
    @Transactional
    public Lld findLldDetail(Long id) {
        return this.lldDao.get(id);
    }

    /**
     * 查询领料单明细
     *
     * @param lldId
     * @return
     */
    public String findLldmxDetail(Long lldId) {
        Gson gson = new Gson();
        List<Map> lldmxList = this.lldDao.findLldmxList(lldId);
        Double kdsl = 0d;
        Double kdje = 0d;
        Double flsl = 0d;
        Double flje = 0d;
        String result = "";
        if (lldmxList.size() > 0) {
            for (Map map : lldmxList) {
                kdsl += (Double) map.get("kdsl") == null ? 0d : (Double) map.get("kdsl");
                kdje += (Double) map.get("kdje") == null ? 0d : (Double) map.get("kdje");
                flsl += (Double) map.get("flsl") == null ? 0d : (Double) map.get("flsl");
                flje += (Double) map.get("flje") == null ? 0d : (Double) map.get("flje");
            }
            result = "{\"rows\":" + gson.toJson(lldmxList) + ",\"footer\":[{\"wzgg.code\":\"合计:\",\"kdsl\":" + kdsl + ",\"kdje\":" + kdje + ",\"flsl\":" + flsl + ",\"flje\":" + flje + "}]}";
        } else {
            result = "{\"rows\":" + gson.toJson(lldmxList) + "}";
        }
        return result;
    }

    /**
     * 查询领料单明细
     *
     * @param khId
     * @return
     */
    public String findLldmxDetailByKh(Long khId) {
        Gson gson = new Gson();
        List<Map> lldmxList = this.lldDao.findLldmxListByKh(khId);
        Double kdsl = 0d;
        Double kdje = 0d;
        Double flsl = 0d;
        Double flje = 0d;
        String result = "";
        if (lldmxList.size() > 0) {
            for (Map map : lldmxList) {
                kdsl += (Double) map.get("kdsl") == null ? 0d : (Double) map.get("kdsl");
                kdje += (Double) map.get("kdje") == null ? 0d : (Double) map.get("kdje");
                flsl += (Double) map.get("flsl") == null ? 0d : (Double) map.get("flsl");
                flje += (Double) map.get("flje") == null ? 0d : (Double) map.get("flje");
            }
            result = "{\"rows\":" + gson.toJson(lldmxList) + ",\"footer\":[{\"wzgg.code\":\"合计:\",\"kdsl\":" + kdsl + ",\"kdje\":" + kdje + ",\"flsl\":" + flsl + ",\"flje\":" + flje + "}]}";
        } else {
            result = "{\"rows\":" + gson.toJson(lldmxList) + "}";
        }
        return result;
    }

    /**
     * 保存领料单
     *
     * @param lld
     */
    @Transactional
    public void saveLld(Lld lld) throws SQLException {
        lld.setLldh(bmService.getLldbm());
        lld.setKdrq(new Date());
        lld.setZt("ybc");
        lld.setKdr(YgService.getCurrentYgName());
        lld.setCreateUser(YgService.getCurrentYg());
        lld.setCreateOrgan(YgService.getCurrentYg().getZggs());
        lld.setCreateDepartment(YgService.getCurrentYg().getZgjg());
        lld.setDateCreated(new Date());
        lld.setLastUpdated(lld.getDateCreated());
        initLldmx(lld.getLldmxList());
        lldDao.save(lld);
    }

    //过滤领料单明细不必填元素
    private void initLldmx(List<Lldmx> lldmxList) {
        if (lldmxList != null && lldmxList.size() != 0) {
            for (Lldmx lldmx : lldmxList) {
                if (lldmx.getJzfs() != null && lldmx.getJzfs().getId() == null) {
                    lldmx.setJzfs(null);
                }
                lldmx.setKdje(lldmx.getKddj() * lldmx.getKdsl());
                lldmx.setCreateUser(YgService.getCurrentYg());
                lldmx.setCreateOrgan(YgService.getCurrentYg().getZggs());
                lldmx.setCreateDepartment(YgService.getCurrentYg().getZgjg());
                lldmx.setLastUpdated(lldmx.getDateCreated());
            }
        }
    }

    /**
     * 审核领料单
     *
     * @param id
     */
    @Transactional
    public void changeStatus(Long id) {
        Lld lld = this.findLldDetail(id);
        lld.setZt("ysp");
        lld.setShr(YgService.getCurrentYg());
        this.lldDao.save(lld);
        for (Lldmx lldmx : lld.getLldmxList()) {
            Kh kh = khDao.get(lldmx.getKh().getId());
            kh.setKkdsl(kh.getKkdsl() - lldmx.getKdsl());
            khDao.save(kh);
        }
    }

    /**
     * 办理领料单
     *
     * @param lld
     */
    @Transactional
    public void blLld(Lld lld) {
        Lld lldItem = this.lldDao.get(lld.getId());
        lldItem.setFlr(YgService.getCurrentYg());
        lldItem.setZt("yfl");
        lldItem.setFlrq(new Date());
        lldItem.setLastUpdated(new Date());
        this.lldDao.save(lldItem);
        List<Lldmx> lldmxList = lldItem.getLldmxList();
        for (int i = 0; i < lld.getLldmxList().size(); i++) {
            for (Lldmx lldmx : lldmxList) {
                if (lldmx.getId().equals(lld.getLldmxList().get(i).getId())) {
                    lldmx.setFljs(lld.getLldmxList().get(i).getFljs());
                    lldmx.setFlsl(lld.getLldmxList().get(i).getFlsl());
                    lldmx.setFldj(lld.getLldmxList().get(i).getKddj());
                    lldmx.setLlbz(lld.getLldmxList().get(i).getLlbz());
                    lldmx.setFlje(lld.getLldmxList().get(i).getFlsl() * lld.getLldmxList().get(i).getKddj());
                    lldmx.setLastUpdated(new Date());
                    lldmx.getKh().setSl(lldItem.getLldmxList().get(i).getKh().getSl() - lldItem.getLldmxList().get(i).getFlsl());
                    lldmx.getKh().setKkdsl(lldItem.getLldmxList().get(i).getKh().getKkdsl() + lldItem.getLldmxList().get(i).getKdsl() - lldItem.getLldmxList().get(i).getFlsl());
                    break;
                }
            }
        }
    }

    //==================get and set=====================
    @Autowired
    public void setLldDao(LldDao lldDao) {
        this.lldDao = lldDao;
    }

    @Autowired
    public void setLldmxDao(LldmxDao lldmxDao) {
        this.lldmxDao = lldmxDao;
    }

    @Autowired
    public void setKhDao(KhDao khDao) {
        this.khDao = khDao;
    }

    @Autowired
    public void setBmService(BmService bmService) {
        this.bmService = bmService;
    }
}
